#!/bin/bash
# how to install
# bash <(curl https://gitee.com/satai/cangling/raw/master/install.sh)
# 当部署的时候 将此文件安装到服务器上 比如 gitee
clear
echo "                               CANGLING.CN INSTALL                             "
echo "--------------------------------------------------------------------------------"
echo ""

# 下载镜像
function repo() {
  echo "   downloading docker image "
  ALIYUN_SERVER=registry.cn-beijing.aliyuncs.com
  read -p "aliyun repository password:" PASS
  docker login --username=zhangjianshe@gmail.com -p${PASS} ${ALIYUN_SERVER}

  # Check if the script has been given an argument
  if [ $# -eq 0 ]; then
      echo "Usage: $0 {pull|push}"
      exit 1
  fi

  # Define the list of images
  IMAGES="hub.cangling.net/components/zookeeper:latest
          hub.cangling.net/components/kafka:latest
          hub.cangling.net/mapway/postgis:13.0
          hub.cangling.net/mapway/postgis:17
          hub.cangling.net/mapway/emqx:v1.0
          hub.cangling.net/mapway/redis:v1
          hub.cangling.net/tools/nginx:1.14.2
          hub.cangling.net/mapway/mongo:5.0.3
          hub.cangling.net/mapway/influxdb:2
          hub.cangling.net/mapway/ai-geoserver:1.0
          hub.cangling.net/mapway/cis-im:1.0
          hub.cangling.net/mapway/cis-k8s:1.0
          hub.cangling.net/mapway/cis-map:1.0
          hub.cangling.net/mapway/cis-server:1.0"

  # Loop through each image
  for IMAGE in $IMAGES; do
      # Extract the original image name and tag
      ORIGINAL_NAME=$(echo $IMAGE | awk -F/ '{print $NF}')

      # Define the new image name (you can modify this part)
      NEW_NAME="${ALIYUN_SERVER}/mapway/${ORIGINAL_NAME}"

      if [ "$1" = "pull" ]; then
          # Pull the original image
          echo "Pulling $NEW_NAME..."
          docker pull $NEW_NAME
          docker tag  $NEW_NAME $IMAGE

      elif [ "$1" = "push" ]; then
          # Tag the image with the new name
          echo "Tagging $IMAGE as $NEW_NAME..."
          docker tag $IMAGE $NEW_NAME

          # Push the new image to the registry
          echo "Pushing $NEW_NAME..."
          docker push $NEW_NAME

      else
          echo "Unknown command: $1"
          echo "Usage: $0 {pull|push}"
          exit 1
      fi
  done
}

repo pull
read -p "input your target directory<default is ~/ib>:" HOST_TARGET_DIR

if [ -z "$HOST_TARGET_DIR" ]; then
  HOST_TARGET_DIR=~/ib
fi

#check directory exist
if [ ! -d "$HOST_TARGET_DIR" ]; then
  mkdir -p $HOST_TARGET_DIR
fi

docker run --user $(id -u):$(id -g) --net host --rm --name  install -v"$HOST_TARGET_DIR:/installtarget" hub.cangling.net/mapway/cis-server:1.0 ./install.sh